package com.scqkxx.gamebox.activity.news;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.news.adapter.NewsDetailAdapter;
import com.scqkxx.gamebox.databinding.ActivityNewsDetailBinding;
import com.scqkzqtz.base.base.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class NewsDetailActivity extends BaseActivity<ActivityNewsDetailBinding> {

    private List<AVObject> dataList = new ArrayList<>();
    private NewsDetailAdapter newsDetailAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_news_detail;
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener((int titleType) -> finish());

        newsDetailAdapter = new NewsDetailAdapter(R.layout.item_news_marks,dataList);
        binding.rvNewList.setLayoutManager(new LinearLayoutManager(this));
        binding.rvNewList.setAdapter(newsDetailAdapter);

        int newsId = getIntent().getIntExtra("newsId",0);
        int goodsId = getIntent().getIntExtra("goodsId",0);
        getGoods(goodsId);
        getNews(newsId);
    }

    private void getGoods(int goodsId){
        AVQuery<AVObject> query = AVQuery.getQuery("Goods");
        query.whereEqualTo("objectId",goodsId);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("Goods",avObjects+"");
                if(avException == null && avObjects != null && avObjects.size() > 0){
                    AVObject avObject = avObjects.get(0);
                    binding.tvNewsName.setText(avObject.getString("name"));
                    binding.tvNewsTime.setText(new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(avObject.getUpdatedAt()));
                }
            }
        });
    }

    private void getNews(int newsId){
        AVQuery<AVObject> query = AVQuery.getQuery("News");
        query.whereEqualTo("objectId",newsId);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("News",avObjects+"");
                if(avException == null && avObjects != null && avObjects.size() > 0){
                    dataList.clear();
                    dataList.addAll(avObjects);
                    newsDetailAdapter.notifyDataSetChanged();
                }
            }
        });
    }

}
